import numpy as np
import random
import string

global users
users = []

def databefore():
    ##先准备数据

    for i in range(2):
        id = random.sample(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], 8)
        password = random.sample(["a", "b", "c", "d", "e", "f", "g", "h"], 8)
        country = random.sample(["中国", "英国", "美国"], 1)
        province = random.sample(["浙江省", "河南省", "江苏省", "北京市"], 1)
        id = ''.join(id)
        password = ''.join(password)
        country = ''.join(country)
        street = ''
        housenumber = ''
        totalmoney=0
        arcticle = {'国家': country, '省份': province, '街道':street, '门牌号': housenumber}

        user = {'id': id, 'name': '', 'password': password, 'article': arcticle, 'totalmoney':totalmoney}
        users.append(user)


def menu():
    print('*********************************')
    print('*         中国工商银行             *')
    print('*         账户管理系统             *')
    print('*         v1.0                   *')
    print('*********************************')
    print('                                 ')
    print('*1.开户                           *')
    print('*2.存钱                           *')
    print('*3.取钱                           *')
    print('*4.转账                           *')
    print('*5.查询                           *')
    print('*6.Bye!                          *')
    a = input('请输入您选择的业务：')
    if (a == '1'):
        register()
    if (a == '2'):
        idnumber=input('请输入你的账户：')
        money=input('请输入你要存储的金额')
        money=float(money)
        deposit(idnumber,money)
    if (a == '3'):
        idnumber=input('请输入您的账户：')
        password=input('请输入您的密码：')
        money=input('请输入您想取出金额：')
        money=float(money)
        withdraw(idnumber,password,money)
    if (a == '4'):
        inidnumber=input('请输入您要转入账户：')
        outidnumber=input('请输入您的账户：')
        outpassword=input('请输入您的密码：')
        outmoney=input('请输入您要转出金额：')
        outmoney=float(outmoney)
        transfer(inidnumber,outidnumber,outpassword,outmoney)
    if (a == '5'):
        idnumber=input('请输入账号：')
        password=input('请输入密码：')
        inquire(idnumber,password)
    if (a == '6'):
        exitsystem()
def register():
    databefore()
    if (len(users) == 100):
        print('系统用户已满！')
    else:
        while (True):
            idnumber = input('请创建您的8位账户:')
            count = 0
            for i in users:
                count += 1
                if (list(i.values())[0] == idnumber):

                    text = input("选择重新创建用户或者退出，选择c继续或者e退出并返回主界面:")
                    if (text == 'c'):
                        break
                    if (text == 'e'):
                        return menu()
                if ((list(i.values()) != idnumber) and count == len(users)):
                    print(users)
                    id = idnumber
                    password = input('请输入需要创建的密码：')
                    country = input('请输入国家：')
                    province = input('请输入省份：')
                    street = input('请输入街道：')
                    housenumber = input('请输入门牌号：')
                    totalmoney=float(input('请输入存款：'))
                    arcticle = {'国家': country, '省份': province, '街道': street, '门牌号': housenumber}
                    user = {'id': id, 'name': '', 'password': password, 'article': arcticle,'totalmoney':totalmoney}
                    users.append(user)
                    print('创建成功！')
def deposit(idnumber,money):
    for i in users:
        if(idnumber==list(i.values())[0]):
            i['totalmoney']+=money
            print('存储成功！')
            return menu()
    print('用户不存在')
def withdraw(idnumber,password,money):
    for i in users:
        if(idnumber==list(i.values())[0]):
            print('账号正确')
            if(password==list(i.values())[2]):
                print('密码正确')
                if(money>list(i.values())[-1]):
                    print('钱不够!')
                    return
                else:
                    i['totalmoney']-=money
                    print('可用余额为：',i['totalmoney'])
                    return
            else:
                print('密码错误！')
                return
    print('账号错误！')
    return menu()
def transfer(inidnumber,outidnumber,outpassword,outmoney):
    flag1,flag2,count,k=0,0,-1,-1            ##这里读者可以试试for index,value in enumerate(users)试试,不使用count)
    for i in users:
        count+=1
        if (inidnumber == list(i.values())[0]):
                print('转入账号存在！')
                flag1=1
        if(outidnumber==list(i.values())[0]):
                tempnumber = list(i.values())[2]
                tempmoney = list(i.values())[-1]
                flag2=1
                k = count
                print('转出账号存在！')
    if(flag1==1 and flag2==1):
        if(tempnumber==outpassword):
            if(outmoney>tempmoney):
                print('金钱不够！')
            else:
                users[k]['totalmoney']-=outmoney
                print('转账成功！',users[k]['totalmoney'])
    else:
        print('账号不存在！')
        return menu()

def inquire(idnumber,password):
    for i in users:
        if(list(i.values())[0]==idnumber):
            if(list(i.values())[2]==password):
                print(i)
                return menu()
            else:
                print('密码输入错误')
                return inquire()
    print('该用户不存在。')
def exitsystem():
    exit(0)

menu()


